package com.sqy.read;

import com.sqy.common.Num;
import org.apache.poi.hslf.extractor.PowerPointExtractor;
import org.apache.poi.sl.extractor.SlideShowExtractor;
import org.apache.poi.sl.usermodel.SlideShow;
import org.apache.poi.xslf.usermodel.*;

import java.io.*;
import java.util.List;

public class ReadPPTx {
    public static void read(String path, String encoding) throws IOException {
        File file=new File(path);

        InputStream input = new FileInputStream(path);
        XMLSlideShow xss = new XMLSlideShow(input);
        //得到全部文本
        String pptText = readBySlideShowExtractor(xss);
        //System.out.println(pptText);
        //hss.getPictureData(); 返回此幻灯片的所有图片。
        List<XSLFPictureData> xslfPictureDataList = xss.getPictureData();
        //输出图片
        /*  for (XSLFPictureData xslfPictureData : xslfPictureDataList){
                byte[] data = xslfPictureData.getData();
                FileOutputStream out = new FileOutputStream("D:\\a大四上\\保密检查\\document\\文档\\Exam\\1\\" + UUID.randomUUID() + ".jpg");
                out.write(data);
                out.close();
            }
        */
        //HSLFSlide 表示一张幻灯片对象
        //hss.getSlides(); 返回幻灯片中找到的所有普通幻灯片
        List<XSLFSlide> xslfSlideList = xss.getSlides();
        FileWriter writer;
        String parentPath = file.getParent(); // null
        File parentDir = file.getParentFile(); // null
        writer = new FileWriter(Num.PATH);
        for (XSLFSlide xslfSlide : xslfSlideList) {
            //HSLFShape表示工作表中包含的所有形状（幻灯片或注释）
            List<XSLFShape> xslfShapeList = xslfSlide.getShapes();
            for (XSLFShape xslfShape : xslfShapeList) {
                if (xslfShape instanceof XSLFTextShape) {
                    //       System.out.println("XSLFTextShape");
                    String text = ((XSLFTextShape) xslfShape).getText();
                    //  System.out.println(text);

                    writer.write(text);
                    writer.flush();


                }
                if (xslfShape instanceof XSLFTable) {
                    //       System.out.println("HSLFTable");
                    int rowSize = ((XSLFTable) xslfShape).getNumberOfRows();
                    int columnSize = ((XSLFTable) xslfShape).getNumberOfColumns();
                    for (int rowNum = 0; rowNum < rowSize; rowNum++) {
                        for (int columnNum = 0; columnNum < columnSize; columnNum++) {
                            XSLFTableCell cell = ((XSLFTable) xslfShape).getCell(rowNum, columnNum);
                            if (cell != null) {
                                String text = cell.getText();
                                //System.out.println(text);


                                writer.write(text);
                                writer.flush();


                            }
                        }
                    }
                }
            }
        }
        ReadTxt.read(Num.PATH, encoding);
        writer.close();


    }


    /**
     * 得到文档的全部的文本,包括页脚，不包括批注
     *
     * @param slideShow ppt/pptx文档对象
     * @return 全部文本
     */
    public static String readBySlideShowExtractor(SlideShow slideShow) {
        SlideShowExtractor slideShowExtractor = new SlideShowExtractor(slideShow);
        return slideShowExtractor.getText();
    }

}
